<?php
/**
 * Created by ThinkPHP5.0.22.
 * User: 小涛.
 * Date: 2018/12/24.
 * Time: 23:04.
 */

namespace app\admin\controller;

use app\admin\model\GoodsBanner as GoodsBannerModel;
use think\Request;

class GoodsBanner extends Admin
{
    public function index(){
        $list = GoodsBannerModel::listBanner();
        $this -> assign("list",$list);
        return $this->fetch();
    }

    public function Sort(){
        $request = Request::instance();
        if($request -> isPost()){
            if($this->verify_authority() != ""){
                return $this->verify_authority();
            }
            $data = $request -> param();
            $edit = new GoodsBannerModel();
            $data['update_time'] = time();
            $edit = $edit -> allowField(true) -> save($data,['id' => $data['id']]);
            if($edit){
                return "排序成功";
            }
            return "排序失败";
        }
    }

    public function add(){
        $request = Request::instance();
        $data = $request -> param();
        $id = 0;
        if (array_key_exists("id",$data)){
            $id = $data["id"];
        }
        $edit = GoodsBannerModel::get($id);
        if($request -> isPost()){
            if($this->verify_authority() != ""){
                return $this->verify_authority();
            }
            $data["update_time"] = time();
            $edit = new GoodsBannerModel();
            $edit = $edit -> allowField(true) -> save($data,['id' => $id]);
            if($edit){
                return "更新成功";
            }
            return "更新失败";
        }
        if(!$edit){
            $this->redirect("admin/Banner/index");
        }
        $this->assign("banner",$edit);
        return $this -> fetch();
    }
}